package com.li.controller.listener;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: rocketmq_boot
 * @description:   %DLQ%common_product_group_name   %DLQ% + 消费topic对应的 消费者组
 *                  死信队列消息默认保持 3天
 * @author: li
 * @create: 2022-09-28 17:05
 **/

@Component
@RocketMQMessageListener(topic = "%DLQ%common_product_group_name", consumerGroup = "dlq_product_group_name")
public class DLQMessageConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private DefaultMQProducer defaultMQProducer;

    @Override
    public void onMessage(MessageExt message) {
        System.out.println("DLQMessageConsumer start: message " + new String(message.getBody()));
        try {
            String body = new String(message.getBody());
            message.setBody((body + "_DLQ").getBytes());
            message.setTopic("TOPIC_TEST");
            defaultMQProducer.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
